package com.yinhe.module.cjl;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhRiskProject;
import com.yinhe.system.service.IYhRiskProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/module/cjl/project")
public class CjlRiskProjectController extends BaseController{
    @Autowired
    private IYhRiskProjectService yhRiskProjectService;

    /**
     * 查询项目风险列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhRiskProject yhRiskProject)
    {
        startPage();
        List<YhRiskProject> list = yhRiskProjectService.selectYhRiskProjectList(yhRiskProject);
        return getDataTable(list);
    }

    /**
     * 导入项目风险列表
     */
    @Log(title = "项目风险", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('module:cjl:project:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<YhRiskProject> util = new ExcelUtil<YhRiskProject>(YhRiskProject.class);
        List<YhRiskProject> riskProjectList = util.importExcel(file.getInputStream());
        String message = yhRiskProjectService.importRiskProject(riskProjectList, updateSupport);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<YhRiskProject> util = new ExcelUtil<YhRiskProject>(YhRiskProject.class);
        util.importTemplateExcel(response, "项目风险数据");
    }

    /**
     * 导出项目风险列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:project:export')")
    @Log(title = "项目风险", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhRiskProject yhRiskProject)
    {
        List<YhRiskProject> list = yhRiskProjectService.selectYhRiskProjectList(yhRiskProject);
        ExcelUtil<YhRiskProject> util = new ExcelUtil<YhRiskProject>(YhRiskProject.class);
        util.exportExcel(response, list, "项目风险数据");
    }

    /**
     * 获取项目风险详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhRiskProjectService.selectYhRiskProjectById(id));
    }

    /**
     * 新增项目风险
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:project:add')")
    @Log(title = "项目风险", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhRiskProject yhRiskProject)
    {
        long represent = Long.parseLong(yhRiskProject.getRepresentativeOffice());
        yhRiskProject.setDeptId(represent);
        return toAjax(yhRiskProjectService.insertYhRiskProject(yhRiskProject));
    }

    /**
     * 修改项目风险
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:project:edit')")
    @Log(title = "项目风险", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhRiskProject yhRiskProject)
    {
        long represent = Long.parseLong(yhRiskProject.getRepresentativeOffice());
        yhRiskProject.setDeptId(represent);
        return toAjax(yhRiskProjectService.updateYhRiskProject(yhRiskProject));
    }

    /**
     * 删除项目风险
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:project:remove')")
    @Log(title = "项目风险", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhRiskProjectService.deleteYhRiskProjectByIds(ids));
    }
}
